/**
 * Created by Administrator on 2017-07-06.
 */
// 引入自定义函数参考页面
import _ from 'lodash'
import md5 from '../../../static/MD5'
import store from '../../store/index.js'
export default {
    install (Vue, options) {
        Vue.prototype.axiosDownloadHeaders = () => {
            let timestamp = new Date().getTime()
            timestamp = timestamp + ''
            return {
                Authorization: localStorage.AuthorizationToken2,
                timestamp: timestamp,
                loginType: '1',
                signature: md5(localStorage.AuthorizationToken2 + timestamp + localStorage.AuthorizationTokenKey).toUpperCase()
            }
        } // 返回axios下载的header
        Vue.prototype.dateToString2 = (date) => {
            return date.getFullYear() + '-' + (date.getMonth() + 1).toString() + '-' + (date.getDate()).toString() + ' 00:00:00'
        } // date转字符串
        Vue.prototype.dateToString3 = (date) => {
            return date.getFullYear() + '-' + (date.getMonth() + 1).toString() + '-' + (date.getDate()).toString() + ' 23:59:59'
        } // date转字符串
        Vue.prototype.dateToString4 = (date) => {
            return date.getFullYear() + '-' + (date.getMonth() + 1).toString() + '-' + (date.getDate()).toString() +
                ' ' + (date.getHours()).toString() + ':' + (date.getMinutes()).toString() + ':' + (date.getSeconds()).toString()
        } // date转字符串
        Vue.prototype.dateToStr = (date) => {
            return date.getFullYear() + '-' + Vue.prototype.outTenUtils(date.getMonth() + 1) + '-' + Vue.prototype.outTenUtils(date.getDate())
        } // date日期转字符串
        Vue.prototype.outTenUtils = (item) => {
            if (item < 10) {
                return '0' + item
            } else {
                return item
            }
        } // 判断是否大于9，用于处理只有一位的时间数值
        Vue.prototype.isChineseChar = (str) => {
            let reg = /[\u4E00-\u9FA5\uF900-\uFA2D]/
            return reg.test(str)
        } // 验证非汉字，汉字为true

        Vue.prototype.isNumChar = (str) => {
            let reg = new RegExp('^[0-9]*$')
            return !reg.test(str)
        } // 验证非数字字，数字为false
        Vue.prototype.isNumChar2 = (str) => {
            let reg = new RegExp('^-?\\d+$')
            return !reg.test(str)
        } // 验证非浮点数

        Vue.prototype.swap = (arr, index1, index2) => {
            arr[index1] = arr.splice(index2, 1, arr[index1])[0]
            return arr
        } // 数组元素交换

        Vue.prototype.swapUp = (arr, $index) => {
            if ($index === 0) {
                return
            }
            Vue.prototype.swap(arr, $index, $index - 1)
        } // 数组元素上移

        Vue.prototype.swapDown = (arr, $index) => {
            if ($index === arr.length - 1) {
                return
            }
            Vue.prototype.swap(arr, $index, $index + 1)
        } // 数组元素下移

        Vue.prototype.copyArr = (arr) => {
            return _.cloneDeep(arr)
        } // 数组深拷贝
        // ****************** 打印方法*****************************************
        // Vue.prototype.$C = (x) => {
        // 	console.log('打印方法：' + x)
        // }
        // ****************** 嵌套打印方法*****************************************
        // Vue.prototype.$XX = (x) => {
        // 	Vue.prototype.$C(x)
        // }
        Vue.prototype.$ND = () => {
            let date = new Date()
            let Y = date.getFullYear()
            let m = date.getMonth() + 1
            let d = date.getDate()
            // let H = date.getHours()
            // let i = date.getMinutes()
            // let s = date.getSeconds()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            // if (i < 10) {
            //     i = '0' + i
            // }
            // if (s < 10) {
            //     s = '0' + s
            // }
            return Y + '-' + m + '-' + d
        }
        Vue.prototype.$NDT = (add) => {
            let date = new Date()
            let Y = date.getFullYear() + add
            let m = date.getMonth() + 1
            let d = date.getDate()
            // let H = date.getHours()
            // let i = date.getMinutes()
            // let s = date.getSeconds()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            // if (i < 10) {
            //     i = '0' + i
            // }
            // if (s < 10) {
            //     s = '0' + s
            // }
            return Y + '-' + m + '-' + d
        }
        // *******************时间格式化的方法*************************************
        Vue.prototype.$F = (dateNum) => {
            if (!dateNum) {
                return ''
            }
            let date = new Date(parseInt(dateNum))
            let Y = date.getFullYear()
            let m = date.getMonth() + 1
            let d = date.getDate()
            let H = date.getHours()
            let i = date.getMinutes()
            let s = date.getSeconds()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            if (i < 10) {
                i = '0' + i
            }
            if (s < 10) {
                s = '0' + s
            }
            return Y + '-' + m + '-' + d + ' ' + H + ':' + i + ':' + s
        }
        Vue.prototype.$FAddY = (dateNum, addYear) => {
            if (!dateNum) {
                return ''
            }
            let date = new Date(parseInt(dateNum))
            let Y = date.getFullYear() + addYear
            let m = date.getMonth() + 1
            let d = date.getDate()
            let H = date.getHours()
            let i = date.getMinutes()
            let s = date.getSeconds()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            if (i < 10) {
                i = '0' + i
            }
            if (s < 10) {
                s = '0' + s
            }
            return Y + '-' + m + '-' + d + ' ' + H + ':' + i + ':' + s
        }
        Vue.prototype.$FAddYS = (dateNum, addYear) => {
            if (!dateNum) {
                return ''
            }
            let date = new Date(parseInt(dateNum))
            let Y = date.getFullYear() + addYear
            let m = date.getMonth() + 1
            let d = date.getDate()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            return Y + '-' + m + '-' + d
        }
        Vue.prototype.precision = (money) => {
            return (parseFloat(money) / 100).toFixed(2) + ''
        } // 单位为分的金钱格式化为元为单位的数字，保留小数点后两位
        Vue.prototype.$FS = (dateNum) => {
            if (!dateNum) {
                return ''
            }
            let date = new Date(parseInt(dateNum))
            let Y = date.getFullYear()
            let m = date.getMonth() + 1
            let d = date.getDate()
            if (m < 10) {
                m = '0' + m
            }
            if (d < 10) {
                d = '0' + d
            }
            return Y + '-' + m + '-' + d
        }
        Vue.prototype.$overTime = (dateNum) => {
            if (parseInt(new Date().getTime()) > dateNum) {
                return false
            } else {
                return true
            }
        }
        // *******************时间格式化的方法*************************************
        Vue.prototype.$IN7DAY = (dateNum) => {
            let nowDate = Date.parse(new Date())
            let less7Days = dateNum - (7 * 24 * 60 * 60 * 1000)
            if (nowDate < dateNum && less7Days < nowDate) {
                return true
            } else {
                return false
            }
        }
        Vue.prototype.$overTenYear = (dateNum1, dateNum2) => {
            let date = new Date(parseInt(dateNum1))
            let Y = date.getFullYear() + 10
            date.setFullYear(Y)
            if (date.getTime() > dateNum2) {
                return false // 十年以内
            } else {
                return true // 超过十年
            }
        }
        // 时间戳转时间格式
        Vue.prototype.timestampToTime = (timestamp) => {
            var date = new Date(timestamp) // 时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-'
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
            var D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate()) + ' '
            //  var h = (date.getHours() < 10 ? '0' + date.getHours() : date.getHours()) + ':'
            //  var m = (date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()) + ':'
            //  var s = (date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds())
            return Y + M + D
        }
        // 两位小数
        Vue.prototype.$fix2 = (num) => {
            return Math.round(num * 100) / 100
        }
        // 获取 数据类型
        Vue.prototype.$getDataType = (data) => {
            return Object.prototype.toString.call(data).slice(8, -1)
        }
        Vue.prototype.$formatNumber = (n) => {
            const str = n.toString()
            return str[1] ? str : `0${str}`
        }
        Vue.prototype.returnTimestampSignature = (companyCode) => {
            if (!companyCode) {
                companyCode = store.state.companyCode
            }
            let desKey = 'krixh27rnfis9567wnr83n48'
            let timestamp = new Date().getTime()
            let signature = md5(companyCode + timestamp + desKey).toUpperCase()
            return {
                timestamp,
                signature
            }
        }
        // ******************* 时间格式化的方法 (允许空值，默认为当前) *************************************
        Vue.prototype.$dateFormat = (date, format = 'Y-M-D h:m:s') => {
            if (!date) {
                date = new Date()
            }
            if (Vue.prototype.$getDataType(date) === 'String') {
                date = date.replace(/-/g, '/')
            }
            if (Vue.prototype.$getDataType(date) !== 'Date') {
                date = new Date(date)
            }
            const year = date.getFullYear()
            const month = date.getMonth() + 1
            const day = date.getDate()
            const hour = date.getHours()
            const minute = date.getMinutes()
            const second = date.getSeconds()

            let result = format
            result = result.replace('Y', year)
            result = result.replace('M', Vue.prototype.$formatNumber(month))
            result = result.replace('D', Vue.prototype.$formatNumber(day))
            result = result.replace('h', Vue.prototype.$formatNumber(hour))
            result = result.replace('m', Vue.prototype.$formatNumber(minute))
            result = result.replace('s', Vue.prototype.$formatNumber(second))
            return result
        }
        Vue.prototype.$netUrl = (name, type = 0) => {
            if (type === 1) {
                // return 'https://lybarofflineupdata.oss-cn-shenzhen.aliyuncs.com/templet/crm/skip/liyuhealth/' + name + '.' + type
                return 'https://lybarofflineupdata.oss-cn-shenzhen.aliyuncs.com/templet/xxplatform/zzservice/toagent/' + name
            } else {
                return 'https://lybarofflineupdata.oss-cn-shenzhen.aliyuncs.com/templet/xxplatform/zzservice/tocompany/' + name
            }
        } // 返回图标色调
        // ******************* 时间格式化的方法 (不允许空值，空值返回 '') *************************************
        Vue.prototype.$dateFormat2 = (date, format = 'Y-M-D h:m:s') => {
            if (!date) {
                return ''
            }
            if (Vue.prototype.$getDataType(date) === 'String') {
                date = date.replace(/-/g, '/')
            }
            if (Vue.prototype.$getDataType(date) !== 'Date') {
                date = new Date(date)
            }
            const year = date.getFullYear()
            const month = date.getMonth() + 1
            const day = date.getDate()
            const hour = date.getHours()
            const minute = date.getMinutes()
            const second = date.getSeconds()

            let result = format
            result = result.replace('Y', year)
            result = result.replace('M', Vue.prototype.$formatNumber(month))
            result = result.replace('D', Vue.prototype.$formatNumber(day))
            result = result.replace('h', Vue.prototype.$formatNumber(hour))
            result = result.replace('m', Vue.prototype.$formatNumber(minute))
            result = result.replace('s', Vue.prototype.$formatNumber(second))
            return result
        }
    }
}
